﻿using System.Web.Mvc;
using zeus.Core;
using SharpArch.Web.NHibernate;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using Webdiyer.WebControls.Mvc;

namespace zeus.Web.Controllers
{
    [HandleError]
    public class TestController : Controller
    {
        private readonly IRepository<Test> testRepository;

        public TestController(IRepository<Test> testRepository)
        {
            Check.Require(testRepository != null, "testRepository may not be null");
            this.testRepository = testRepository;
        }

        [Transaction]
        public ActionResult Combo()
        {
            IList<Test> tests = testRepository.GetAll();
            ViewData["combotest"] = new SelectList(tests, "id", "nombre");

            return View();
        }

        [Transaction]
        public ActionResult List()
        {            
            return View(testRepository.GetAll());
        }

        [Transaction]
        public ActionResult ListaMejorada(int? id)
        {
            PagedList<Test> tests = new PagedList<Test>(testRepository.GetAll(), id ?? 1, 3);
            return View(tests);
        }

        [Transaction]
        public ActionResult ListaMejoradaAjax(int? id)
        {
            PagedList<Test> tests = new PagedList<Test>(testRepository.GetAll(), id ?? 1, 3);
            if (Request.IsAjaxRequest())
            {
                System.Threading.Thread.Sleep(1000);
                return PartialView("ListaAjax", tests);
            }

            return View(tests);           
        }

        [Transaction]
        public ActionResult Create(int? id)
        {
            Test test = new Test();

            if (id != null)
            {
                test = testRepository.Get(id.Value);
            }

            return View(test); 
        }

        [Transaction, HttpPost]
        public ActionResult Create(Test test)
        {
            if (ModelState.IsValid && test.IsValid())
                testRepository.SaveOrUpdate(test);

            return View(test);
        }

        [Transaction]
        public ActionResult CreateTest()
        {
            return View();
        }

        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateTest(Test test)
        {
            if (ModelState.IsValid && test.IsValid())
                testRepository.SaveOrUpdate(test);

            return Json("true");
        }

        [Transaction]
        public ActionResult Edit(int id)
        {
            Test test = testRepository.Get(id);
            return View(test);
        }

        [Transaction, HttpPost]
        public ActionResult Edit(Test test)
        {
            if (ModelState.IsValid && test.IsValid())
                testRepository.SaveOrUpdate(test);

            return View(test);
        }

        [HttpPost]
        public ActionResult GetLatLong(int id)
        {
            double[] LatLong = new double[2];

            LatLong[0] = -75;
            LatLong[1] = 0.12345;
            return Json(LatLong);
        }

        [Transaction]
        public ActionResult Ajax()
        {
            return View();
        }

        [Transaction]
        public ActionResult PeopleMonitoreo()
        {
            return View();
        }
        
    }
}
